package org.itboys.core.controller.front;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.core.entity.mongo.SundayWeatherRecord;
import org.itboys.core.service.mongo.front.SundayFrontWeatherRecordService;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Auther: 罗尧坤
 * Date: 2018/8/21
 * Description:
 */

@Controller
@RequestMapping("/sunday/small/weatherRecord")
@Api(value = "weatherRecord", description = "天气记录")
public class SundayFrontWeatherRecordController extends BaseController {

    @Autowired
    private SundayFrontWeatherRecordService weatherRecordService;

    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ApiOperation(value = "保存天气记录")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "storeId", value = "仓库id")
            @RequestParam(value = "storeId", required = true) Long storeId
            ,
            @ApiParam(required = true, name = "storeName", value = "仓库名字")
            @RequestParam(value = "storeName", required = true) String storeName
            ,
            @ApiParam(required = true, name = "weather", value = "天气情况")
            @RequestParam(value = "weather", required = true) String weather
    ){
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date = df.format(new Date());

            SundayWeatherRecord result = new SundayWeatherRecord(date, storeId, storeName, weather);
            weatherRecordService.save(result);
            ResponseMessage.success();
        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/weatherRecord/save----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }

    @RequestMapping(value = "/getWeatherType", method = {RequestMethod.POST})
    @ApiOperation(value = "获取天气类型")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response

    ) {
        try {
            List<String> result = weatherRecordService.getWeatherType();
            ResponseMessage.success(result);
        } catch (Exception e) {
            String  msg  = CommonConstants.ERROMSG;
            Integer code = CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
                //token已过期
            } else if (e.getClass().getSimpleName().equalsIgnoreCase("TokenException")) {
                code = CommonConstants.TOKEN_CODE;
                msg = CommonConstants.TOKENMSG;
            } else {
                logger.error("/sunday/small/weatherRecord/select----e=" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code, msg);
        }
    }


}
